package com.warehouse_management.purchase.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.warehouse_management.config.PageModel;
import com.warehouse_management.purchase.dao.PurchaseDao;
import com.warehouse_management.purchase.model.*;
import com.warehouse_management.purchase.service.PurchaseService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @PACKAGE_NAME: com.warehouse_management.purchase.service.impl
 * @NAME: PurchaseServiceImpl
 * @USER: ZhouYongCheng
 * @DATE: 2022/10/3
 * @TIME: 2:57
 * @Description: 采购单管理控制层实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, Details> implements PurchaseService {

    @Autowired
    private PurchaseDao purchaseDao;

    /**
     * 查询采购单
     * 周永成
     * @return
     */
    @Override
    public PageModel<Purchase> queryPurchaseList(@Param("purchaseId") String CaiGouDan,
                                           @Param("CaiGouFTime") String CaiGouFTime,
                                           @Param("CaiGouLDate") String CaiGouLTime,
                                           @Param("FMoneny")String FMoneny,
                                           @Param("LMoneny") String LMoneny,
                                           @Param("checkStatus") String ShengHe,
                                           PageModel<Purchase> pageModel){

        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?10:pageModel.getPageSize());

        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());

        List<Purchase> purchases = purchaseDao.queryPurchaseList(CaiGouDan,
                                                                CaiGouFTime,
                                                                CaiGouLTime,
                                                                FMoneny,
                                                                LMoneny,
                                                                ShengHe);

        PageInfo<Purchase> usersPageInfo = new PageInfo<>(purchases);
        pageModel.setTotalPage(usersPageInfo.getPages());
        pageModel.setTotalRows(usersPageInfo.getTotal());
        pageModel.setData(usersPageInfo.getList());

        return pageModel;
    }

    /**
     * 查询用户名
     * 周永成
     * @param id 用户id
     * @return
     */
    @Override
    public String queryUserByName(String id) {
        return purchaseDao.queryUserByName(id);
    }

    /**
     * 周永成
     * 查询所有品牌
     * @return
     */
    @Override
    public List<Brand> queryBrandList() {
        return purchaseDao.queryBrandList();
    }

    /**
     * 周永成
     * 更具品牌查询类型集合
     * @return
     */
    @Override
    public List<Type> queryTypeList(String brand) {
        return purchaseDao.queryTypeList(brand);
    }

    /**
     * 周永成
     * 根据类型查询商品集合
     * @param type
     * @return
     */
    @Override
    public List<Product> queryProductList(String type) {
        return purchaseDao.queryProductList(type);
    }

    /**
     * 周永成
     * 查询厂商
     * @return
     */
    @Override
    public List<Firm> queryFirmList(String productId) {
        return purchaseDao.queryFirmList(productId);
    }

    /**
     * 插入Purchase
     * @param purchase
     * @return
     */
    @Override
    public Boolean insertPurchase(Purchase purchase) {

        return purchaseDao.insertPurchase(purchase);
    }

    /**
     * 插入Details
     * @param details
     * @return
     */
    @Override
    public Boolean insertDetails(Details details) {
        return purchaseDao.insertDetails(details);
    }

    /**
     * 周永成
     * 根据商品id 获取单价
     * @param productId     商品id
     * @return
     */
    @Override
    public String getProductPrice(String productId) {
        return purchaseDao.getProductPrice(productId);
    }

    /**
     * addPurchase分发插入
     * @return
     */
    @Override
    public int FenFaInsert(List<Purchase> purchase,List<Details> details){

        int sum = 0;//总金额

//        HashMap<String, Purchase> map = new HashMap<>();

        int newprice = 0;
        int oldprice = 0;

        /**
         * 循环累加金额
         */
        oldprice = Integer.parseInt(purchase.get(0).getTotalMoney());
        Boolean flag = false;
        for (Purchase purchase1 : purchase) {
            if (!flag){
                flag = true;
                continue;
            }
            newprice += Integer.parseInt(purchase1.getTotalMoney());
            purchase1.setTotalMoney(String.valueOf(newprice + oldprice));   //累加金额
        }

        insertPurchase(purchase.get(purchase.size()-1));    //累加后只需要最后一个对象

        for (Details detail : details) {
            insertDetails(detail);
        }

        return 1;

    }

    /**
     * 获取用户名id
     * 周永成
     * @param userName
     * @return
     */
    @Override
    public String getUserID(String userName) {
        return purchaseDao.getUserID(userName);
    }

    /**
     * 根据订单号获取订单详情Details
     * @param purchaseId
     * @return
     */
    @Override
    public List<Details> queryDetailsByPurchase(String purchaseId) {
        return purchaseDao.queryDetailsByPurchase(purchaseId);
    }

    /**
     * 根据订单号获取订单详情Purchase
     * @param purchaseId
     * @return
     */
    @Override
    public Purchase queryPurchase(String purchaseId) {
        return purchaseDao.queryPurchase(purchaseId);
    }
}
